// pages/starsRecom/starsRecom.js
import {
  Get,
  Post,
  JsonPost
} from "../../utils/request.js"
import Common from "../../utils/common.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //table栏信息
    tableInfo: [{
        category_id: '0',
        category_name: "全部"
      },
      {
        category_id: '1',
        category_name: "待付款"
      },
      {
        category_id: '2',
        category_name: "待发货"
      },
      {
        category_id: '3',
        category_name: "待收货"
      },
      {
        category_id: '4',
        category_name: "待评价"
      },
    ],
    action: "0",
    //卡片列表
    list: [],
    //卡片列表
    listInfo: {
      type: '',
      page: 1,
    },
    //已触底
    chudi: false,
    jiazai: false,
    paying:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let str = 'listInfo.type'
    this.setData({
      action: options.type,
      [str]: options.type,
    })
  },



  // 查询列表信息
  getList() {
    let data = this.data.listInfo
    JsonPost('order/list', data).then(res => {
      this.setData({
        list: [...this.data.list,...res.data.data]
      })
      if (!res.data.data.length) {
        this.setData({
          chudi: true,
          jiazai: false
        })
      } else {
        this.setData({
          chudi: false
        })
      }
    })
  },


  //选中table
  actionTable(e) {
    let str = 'listInfo.type'
    let str1 = 'listInfo.page'
    this.setData({
      action: e.currentTarget.dataset.cid,
      [str]: e.currentTarget.dataset.cid,
      [str1]: 1,
      list:[]
    })
    this.getList()
  },


  //跳转至订单详情
  goDetail(e) {
      wx.navigateTo({
        url: '/pages/orderDetail/orderDetail?id=' + e.currentTarget.dataset.id
      })
  },


  //评论id
  goPinLun(e) {
    wx.navigateTo({
      url: '/pages/evaluate/evaluate?row=' + e.currentTarget.dataset.row
    })
  },


  //再次购买
  tooBuy(e) {
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id
    })
  },

  //删除订单
  delOrder(e) {
    let that=this
    wx.showModal({
      title: '提示',
      content: '确认删除?',
      success(res) {
        if (res.confirm) {
          JsonPost('order/cancel',{'order_id':e.currentTarget.dataset.id }).then(resp => {
            that.setData({
              list:[]
            })
            that.getList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //关闭订单
  closeOrder(e) {
    let that=this
    wx.showModal({
      title: '提示',
      content: '确认取消该订单?',
      success(res) {
        if (res.confirm) {
          JsonPost('order/cancel',{'order_id':e.currentTarget.dataset.id }).then(resp => {
            that.setData({
              list:[]
            })
            that.getList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //确认收货
  SHed(e) {
    let that=this
    wx.showModal({
      title: '提示',
      content: '确认收货?',
      success(res) {
        if (res.confirm) {
          JsonPost('order/confirm-goods',{'order_id':e.currentTarget.dataset.id }).then(resp => {
            that.setData({
              list:[]
            })
            that.getList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //申请售后
  goSouhou(e) {
    wx.navigateTo({
      url: '/pages/submitSale/submitSale?row=' + JSON.stringify(e.currentTarget.dataset.row)
    })
  },

  //查看
  souDetail(e) {
    JsonPost('order/logistics',{'order_id':e.currentTarget.dataset.id}).then(log=>{
      wx.showModal({
        title: '物流信息',
        content: '物流单号: '+log.data.logistics_sn+' 物流公司:'+log.data.logistics_name,
        confirmText: '复制',
        success (res) {
          if (res.confirm) {
            wx.setClipboardData({
              data: log.data.logistics_sn,
              success(res) {
        
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    })
  },

  //付款
  pay(e) {
    let that=this
    if (this.data.paying) return
    wx.showLoading({
      title: '发起支付中',
    })
    this.setData({
      paying: true
    })
    JsonPost('payment/wxPay',{'order_sn':e.currentTarget.dataset.id}).then(Pes=>{
      wx.hideLoading()
      wx.requestPayment({
        timeStamp: Pes.data.timeStamp,
        nonceStr: Pes.data.nonceStr,
        package: Pes.data.package,
        signType: 'MD5',
        paySign: Pes.data.paySign,
        success(res) {
          console.log(res, 'success')
          that.setData({
            paying: false,
          })
          that.getList()
        },
        fail(res) {
          console.log(res, 'fail')
          that.setData({
            paying: false,
          })
          wx.showToast({
            title: '支付失败',
            icon: 'none',
            duration: 2000
          })
        }
      })
    })
    that.setData({
      paying: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.chudi) {
      let str = "listInfo.page"
      this.setData({
        [str]: this.data.listInfo.page + 1,
        jiazai: true
      })
      this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})